<?php
/**
 * Created by PhpStorm.
 * User: shiyang
 * Date: 2018/12/17
 * Time: 21:34
 */

namespace App\Http\Controllers\Perm;


use App\Http\Controllers\Controller;
use App\Http\Requests\RoleRequest;
use App\Libraries\DataSource;
use App\Models\Permission;
use App\Models\PermissionRole;
use App\Models\Role;
use App\Models\RoleUser;
use Response;

class RoleController extends Controller
{
    public function index()
    {
        $this->authorize('permission');
        return view('perm.role.index');
    }

    public function table()
    {
        $roles = Role::get();

        $roles->transform(function ($role){
            return [
                'id' => $role->id,
                'name' => $role->name,
                'created_at' => $role->created_at->toDateTimeString(),
            ];
        });

        $ds = new DataSource();
        $ds->data = $roles;

        return Response::json($ds);
    }

    public function create()
    {
        $this->authorize('permission');
        $perms = Permission::get();
        return view('perm.role.create', compact('perms'));
    }

    public function store(RoleRequest $request)
    {
        $this->authorize('permission');
        $input = $request->all();
        $role = Role::create($input);

        $id = $role->id;
        if (isset($input['permission_id'])) {
            foreach ($input['permission_id'] as $permission_id) {
                $data = [
                    'permission_id' => $permission_id,
                    'role_id'=>$id,
                ];
                PermissionRole::create($data);
            }
        }

        \Session::flash('flash_success', '添加成功');
        return redirect('perm/role');
    }

    public function edit($id)
    {
        $this->authorize('permission');
        $perms = Permission::get();

        $permissionRoles= PermissionRole::where('role_id',$id)->pluck('permission_id');

        $role = Role::with('perms')->find($id);
        if ($role == null) {
            \Session::flash('flash_warning', '无此记录');
            return redirect()->back();
        }

        return view('perm.role.edit', compact('role', 'permissionRoles', 'perms'));
    }

    public function update($id, RoleRequest $request)
    {
        $this->authorize('permission');
        $input = $request->all();
        $role = Role::with('perms')->find($id);
        $role->update($input);

        //删除之前所选
        PermissionRole::query()->where('role_id',$id)->delete();
        if (isset($input['permission_id'])) {
            foreach ($input['permission_id'] as $permission_id) {
                $data = [
                    'permission_id' => $permission_id,
                    'role_id'=>$id,
                ];
                PermissionRole::create($data);
            }
        }

        \Session::flash('flash_success', '修改成功!');
        return redirect('perm/role');
    }

    public function destroy($id)
    {
        $this->authorize('permission');
        $role = Role::query()->find($id);
        if (! $role) {
            return response()->json([
                'code' => 404,
                'message' => '未找到记录'
            ]);
        }
        $flag = $role->delete();
        //删除权限和角色的对应关系
        $flag && PermissionRole::query()->where('role_id', $id)->delete();
        $flag && RoleUser::query()->where('role_id', $id)->delete();
        if($flag) {
            return response()->json([
                'code' => 200,
                'message' => 'Success'
            ]);
        }
        return response()->json([
            'code' => 500,
            'message' => '服务器异常'
        ]);
    }
}
